package com.zxmr.domain.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zxmr.CommonInit;
import com.zxmr.db.DBManager;
import com.zxmr.domain.account.AuthorizationByRole;
import com.zxmr.domain.account.Role;
import com.zxmr.domain.component.Action;

public class AuthorizationService {
	
	DBManager db = CommonInit.getDBManager();
	/**
	 * authorize an action to an role
	 * @param role
	 * @param action
	 * @return
	 */
	public boolean addAuthorization(int role,int action)
	{
		AuthorizationByRole ar = new AuthorizationByRole();
		ar.setAction(action);
		ar.setRole(role);
		try {
			db.getMapper().insert("role.addAuthorizationToRole", ar);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * delete an authorization to a role
	 * @param abr
	 * @return
	 */
	public boolean deleteAuthorization(int abr)
	{
		try {
			db.getMapper().delete("role.deleteAuthorization", abr);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * delete an authorization by role and action
	 * @param abr
	 * @return
	 */
	public boolean deleteAuthorization(int role,int action)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		
		para.put("role", role);
		para.put("action", action);
		try {
			db.getMapper().delete("role.deleteAuthorizationByActionAndRole", para);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * query an authorization
	 * @param role
	 * @param action
	 * @return
	 */
	public AuthorizationByRole queryAuthorization(int role,int action)
	{
		AuthorizationByRole abr = new AuthorizationByRole();
		abr.setAction(action);
		abr.setRole(role);
		try {
			@SuppressWarnings("unchecked")
			List<AuthorizationByRole> list = (List<AuthorizationByRole>)db.getMapper().queryForList("role.queryAuthorizationByRoleAndAction", abr);
			if(list == null || list.isEmpty())return null;
			return list.get(0);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<AuthorizationByRole> queryAuthorization(int user,String action,int component)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		
		para.put("user", user);
		para.put("action", action);
		para.put("component", component);
		
		try {
			@SuppressWarnings("unchecked")
			List<AuthorizationByRole> abr = (List<AuthorizationByRole>)db.getMapper().queryForList("role.queryAuthorizationByUserActionCom", para);
			return abr;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * query all authorization for a role
	 * @param role
	 * @return
	 */
	public List<AuthorizationByRole> queryAllAuthorization(int role)
	{
		try {
			@SuppressWarnings("unchecked")
			List<AuthorizationByRole> list = (List<AuthorizationByRole>)db.getMapper().queryForList("role.queryAuthorizationByRole", role);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * query all authorization for a role
	 * @param role
	 * @return
	 */
	public List<AuthorizationByRole> queryAllAuthorizationWithGroup(int role,int company,int group)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		
		para.put("role", role);
		para.put("company", company);
		para.put("group", group);
		
		try {
			@SuppressWarnings("unchecked")
			List<AuthorizationByRole> list = (List<AuthorizationByRole>)db.getMapper().queryForList("role.queryABRWithGroupAction", para);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 *  query all action of one component for a role
	 * @param role
	 * @param component
	 * @return
	 */
	public List<Action> queryAllActionForAnRole(int role,int component)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("component", component);
		para.put("role", role);
		
		try {
			@SuppressWarnings("unchecked")
			List<Action> list = (List<Action>)db.getMapper().queryForList("action.findByComponentAndRole", para);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean addAction(Action action)
	{
		try {
			db.getMapper().insert("action.add", action);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateAction(Action action)
	{
		try {
			db.getMapper().update("action.update", action);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public List<Action> queryAllAction(int component)
	{
		
		try {
			@SuppressWarnings("unchecked")
			List<Action> list = (List<Action>)db.getMapper().queryForList("action.findByComponent", component);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Action> queryAllActionForCompany(int company)
	{
		try {
			@SuppressWarnings("unchecked")
			List<Action> list = (List<Action>)db.getMapper().queryForList("action.findByCompany", company);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<Action> queryAllActionForGroup(int company,int group)
	{
		try {
			Map<String,Object> para = new HashMap<String,Object>();
			para.put("company", company);
			para.put("group", group);
			
			@SuppressWarnings("unchecked")
			List<Action> list = (List<Action>)db.getMapper().queryForList("action.findByGroup", para);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public boolean validateActionByCompany(int company,int action)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("company", company);
		para.put("action", action);
		
		try {
			Action ac = (Action)db.getMapper().queryForObject("action.validateActionByUser", para);
			if(ac == null)return false;
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public boolean validateGroupByCompany(int company,int group)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("company", company);
		para.put("group", group);
		
		try {
			int ac = (Integer)db.getMapper().queryForObject("action.validateGroupByUser", para);
			if(ac == 0)return false;
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public boolean validateActionByCompanyAndComponent(int company,int action,int component)
	{
		Map<String,Object> para = new HashMap<String,Object>();
		para.put("company", company);
		para.put("action", action);
		para.put("component", component);
		
		try {
			int ac = (Integer)db.getMapper().queryForObject("action.validateActionByCompanyAndComponent", para);
			if(ac == 0)return false;
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	public boolean addRole(Role role)
	{
		try {
			db.getMapper().insert("role.add", role);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateRole(Role role)
	{
		try {
			db.getMapper().update("role.update", role);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public boolean deleteRole(Role role)
	{
		try {
			db.getMapper().startTransaction();
			db.getMapper().delete("user.deleteRoleByRoleID", role.getId());
			db.getMapper().delete("role.deleteAuthorizationByRole", role.getId());
			db.getMapper().delete("role.delete", role.getId());
			db.getMapper().commitTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				db.getMapper().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	public List<Role> queryAllRole(int company)
	{
		try {
			@SuppressWarnings("unchecked")
			List<Role> list = (List<Role>)db.getMapper().queryForList("role.findByCompany", company);

			return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	public Role queryRole(int id)
	{
		try {
			Role role = (Role)db.getMapper().queryForObject("role.findByID", id);

			return role;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
